package tests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;

import lp.polimi.treefinder.utils.RankElement;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

import utils.Species;
import featuresProcessing.Detector;
import featuresProcessing.KNNBasedRankGenerator;
import featuresProcessing.MatchingSystem;

public class DBTest {

	public static final int GENERAL_SERVER_PORT = 4445;
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);


		Detector fd = MatchingSystem.getSharedInstance().getFeaturesDetector();
		Detector sd = MatchingSystem.getSharedInstance().getShapeDetector();
		//		Mat image1 = Highgui.imread("leafs/foglia1.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
		//		Mat image2 = Highgui.imread("leafs/foglia2.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
		//		Mat image3 = Highgui.imread("leafs/foglia3.jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
		//		Species s1 = new Species(0, fd.getDescriptors(image1) , sd.getDescriptors(image1), "Foglia1", ImageUtils.imageToByteArray(new ImageIcon("leafs/foglia1.jpg")));
		//		Species s2 = new Species(0, fd.getDescriptors(image2) , sd.getDescriptors(image2), "Foglia2", ImageUtils.imageToByteArray(new ImageIcon("leafs/foglia2.jpg")));
		//		Species s3 = new Species(0, fd.getDescriptors(image3) , sd.getDescriptors(image3), "Foglia3", ImageUtils.imageToByteArray(new ImageIcon("leafs/foglia3.jpg")));
		//		
		//		try {
		//			MatchingSystem.getSharedInstance().addSpeciesToDB(s1);
		//			MatchingSystem.getSharedInstance().addSpeciesToDB(s2);
		//			MatchingSystem.getSharedInstance().addSpeciesToDB(s3);
		//		} catch (SQLException | IOException e) {
		//			// TODO Auto-generated catch block
		//			e.printStackTrace();
		//		}

		
		try {
//			MatchingSystem.getSharedInstance().batchAddSpeciesToDB("Leaves(ICL)/Leaves.txt", "Leaves(ICL)");
			
			
			MatchingSystem.getSharedInstance().fetchDatabase();
			System.out.println("Fetched!");
			while (true) {
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				//001/001 (29)
				Mat testImage = Highgui.imread("Leaves(ICL)/"+br.readLine()+".jpg", Highgui.CV_LOAD_IMAGE_GRAYSCALE);
				Species testSpecies = new Species(0, fd.getDescriptors(testImage), sd.getDescriptors(testImage));
				ArrayList<RankElement> rank = MatchingSystem.getSharedInstance().classify(testSpecies, new KNNBasedRankGenerator());
				for (int i = 0; i < rank.size(); i++) {
					System.out.println(rank.get(i).getName());
				}
			}
//		} catch (ClassNotFoundException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
